package com.wx.resume_excel.config;

import com.wx.resume_excel.utils.ExcelUtil;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

//@Component
//@EnableScheduling   //启动定时器
public class TaskConfig {

    /**
     * 每月的1号的凌晨2点会自动删除本月生成的 xlsx 文档
     */
    //每天凌晨刷新home页面中的news信息数据
    @Scheduled(cron = "0 0 2 1 * ?")
    public void deleteStaticFile(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
        String time = sdf.format(new Date());
        int fileTimes = ExcelUtil.getFileName(time);
        String[] times = time.split(":");
        for(int i = 1 ; i <= fileTimes ; i++){
            String fileName = times[0]+""+times[1];
            if(i<10){
                fileName = fileName+"0"+i;
            }else{
                fileName = fileName + "" + i;
            }

            String filePath = "F:/tns-server/tns-resume-excel/src/main/resources/static/"+fileName+".xlsx";
            File file = new File(filePath);
            if(file.exists()){
                file.delete();
            }
        }
    }
}
